// import axios from 'axios'
// import nprogress from 'nprogress'
import stores from '../store'
import configs from '../static/config.js'
import qs from 'qs'
let token = ''
if (uni.getStorageSync('token')) {

	token = uni.getStorageSync('token');

}
console.log(stores)
const baseUrl = configs.ApiUrl
const service = (api = '', date = {}, type = 'GET', header = {
}) => {
	return new Promise((resolve, reject) => {

		uni.request({
			method: api.method,
			url: baseUrl + api.url,
			data: api.data,
			header: {
				'Content-Type': 'application/json',
				'token': uni.getStorageSync('token')
			},
			dataType: 'json',
		}).then((response) => {
			setTimeout(function () {
				uni.hideLoading();
			}, 200);
			let [error, res] = response;
			if (error == null) {
				if (res.data.code === 401) {
					uni.showModal({
						title: '提示',
						showCancel: false,
						content: '登录过期，请重新登录',
						success: function (res) {
							if (res.confirm) {
								uni.clearStorageSync();
								uni.redirectTo({
									url: '/pages/login/index'
								});
							}
						}
					});
					return
				}
				if (res.data.code === 1) {
					resolve(res.data);
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg,
						duration: 2000
					});
				}
			}
			else {
				uni.showToast({
					title: '请求超时，请重试',
					icon: 'error', //将值设置为 success 或者 ''
					duration: 2000 //持续时间为 2秒
				})
				reject(err)
			}





		})

	});
}
export default service
